import { createRouter, createWebHistory } from 'vue-router'
import Index from "@/views/Index.vue";
// import Dynamic from "@/views/Dynamics.vue";
import History from "@/views/History.vue";
import PersonInfo from "@/views/PersonInfo.vue";
// import Save from "@/views/space/Save.vue";
import Create from "@/views/Create.vue";
import Message from "@/views/Message.vue";
import AtMe from "@/views/message/AtMe.vue";
import SystemNotification from "@/views/message/SystemNotification.vue";
import MyMessage from "@/views/message/MyMessage.vue";
import ReceivedLikes from "@/views/message/ReceivedLikes.vue";
import ReplyToMe from "@/views/message/ReplyToMe.vue";
import MessageSettings from "@/views/message/MessageSettings.vue";
import VideoDetail from "@/views/VideoDetail.vue";
import TestDPlayer from "@/views/TestDPlayer.vue";

import CreateIndex from '@/views/create/Index.vue'
import { useUserStore } from "@/stores/userInfoStore.js";
import {useLoginStore} from "@/stores/LoginStore.js";
import emitter from "@/utils/emitter.js";
import UploadVideo from "@/views/create/UploadVideo.vue";
import CreationManage from "@/views/create/CreationManage.vue";
import CommentManage from "@/views/create/CommentManage.vue";
import BarrageManage from "@/views/create/BarrageManage.vue";
// import baseInfo from "@/views/account/BaseInfo.vue";
import BaseInfo from "@/views/account/BaseInfo.vue";
import AvatarEdit from "@/views/account/AvatarEdit.vue";
import UploadAvatar from "@/views/account/UploadAvatar.vue";
import Channel from "@/views/Channel.vue";
import Chat from "@/views/Chat.vue";
import LikeDetail from "@/views/message/LikeDetail.vue";
import VideoComments from "@/views/create/comment-manage/VideoComments.vue";
import DynamicComments from "@/views/create/comment-manage/DynamicComments.vue";

// import emitter from "@/utils/emitter.js";
const router = createRouter( {
  history: createWebHistory(),
  routes: [
    {
      path:'/',
      component:()=> import('@/layouts/BasicLayout.vue'),
      children:[
        {
          path: 'testeleplayer',
          name:'testeleplayer',
          component:()=> import('@/views/deprecated/TestEleVideo.vue'),
        },
        {
          path: '',
          name: 'index',
          component: Index,
        },

        {
          path: 'chat',
          name: 'chat',
          component: Chat,
        },
        {
          path:'/search',
          name:'search',
          component:()=> import('@/views/Search.vue'),
        },

        {
          path: '/channel',
          name: 'channel',
          component: Channel,
        },
        {
          path: '/dynamics',
          name: 'dynamics',
          component: ()=> import('@/views/Dynamics.vue')
        },
        {
          path: '/d-detail/:dynamicId',
          name: 'd-detail',
          component: ()=> import('@/views/Dynamic.vue')
        },
        {
          path: 'history',
          name: 'history',
          component: History,
        },
        {
          path: 'account',
          name: 'account',
          component: PersonInfo,
          children:[
            {
              path: 'setting',
              name: 'setting',
              component: BaseInfo,
            },{
              path: 'avatar',
              name: 'avatar',
              component: AvatarEdit,
            },{
              path: 'upload',
              name: 'upload',
              component: UploadAvatar,
            }
          ]
        },

        {
          path: 'create',
          name: 'create',
          component: Create,
          children:[
            {
              path: 'index',
              component: CreateIndex
            },{
              path: 'upload',
              component: UploadVideo
            },
            {
              path: 'creation',
              component: CreationManage
            },{
              path: 'comments',
              component: CommentManage,
              children:[
                {
                  path: 'v-c',
                  component: VideoComments,
                },{
                  path: 'd-c',
                  component: DynamicComments,
                }
              ]
            },{
              path: 'barrage',
              component: BarrageManage
            },
            // {
            //   path: 'transaction',
            //   component: Transaction
            // },
          ]
        },
        {
          path: 'message',
          name: 'message',
          component: Message,
          children:[
            {
              path: 'atme',
              name: 'atme',
              component: AtMe,
            },
            {
              path: 'mymessage',
              name: 'mymessage',
              component: MyMessage,
            },
            {
              path: 'receivelikes',
              name: 'receivelikes',
              component: ReceivedLikes,
            },
            {
              path: 'likeDetail',
              name: 'likeDetail',
              component: LikeDetail,
            },
            {
              path: 'systemnotify',
              name: 'systemnotify',
              component: SystemNotification,
            },
            {
              path: 'replytome',
              name: 'replytome',
              component: ReplyToMe,
            },
            {
              path: 'messagesettings',
              name: 'messagesettings',
              component: MessageSettings,
            },
          ]
        },
        {
          path: '/video/:videoId/',
          name: 'video',
          component: VideoDetail,
        },
        {
          path: '/testdplayer',
          name: 'testdplayer',
          component: TestDPlayer,
        },
      ]
    },

    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue'),
    // },
    {
      path: '/space',
      name: 'space',
      component: () => import('@/layouts/SpaceLayout.vue'),
      children:[
        {
          path: 'home/:userId',
          name: 'home',
          component: () => import('@/views/space/Home.vue'),
        },
        {
          path: 'dynamic/:userId',
          name: 'dynamic',
          component: () => import('@/views/space/MyDynamic.vue'),
        },
        {
          path: 'submit/:userId',
          name: 'submit',
          component: () => import('@/views/space/Submit.vue'),
        },
        {
          path: 'save/:userId',
          name: 'save',
          component: () => import('@/views/space/Save.vue'),
        },
        {
          path: 'settings',
          name: 'settings',
          component: () => import('@/views/space/Settings.vue'),
        },
        {
          path: 'relation/:userId',
          name: 'relation',
          component: () => import('@/views/space/Relation.vue'),
          children:[
            {
              path: 'care',
              name: 'care',
              component: () => import('@/views/space/relation/Care.vue'),
            },
            {
              path: 'fans',
              name: 'fans',
              component: () => import('@/views/space/relation/Fans.vue'),
            },
          ]
        },
      ]
    },
    {
      path:"/admin",
      name:'admin',
      component:()=> import("@/layouts/AdminLayout.vue"),
      children:[
        {
          path:"home",
          name:'adminHome',
          component:()=> import("@/views/admin/home/Home.vue"),
        },
        {
          path:"data",
          name:'data',
          component:()=> import("@/views/admin/home/DataCenter.vue"),
        },
        {
          path:"employee",
          name:'employee',
          component:()=> import("@/views/admin/system-management/EmployeeList.vue"),
        },{
          path:"user",
          name:'user',
          component:()=> import("@/views/admin/system-management/UserList.vue"),
        },
        {
          path:"addEmployee",
          name:'addEmployee',
          component:()=> import("@/views/admin/system-management/AddEmployee.vue"),
        },
        {
          path:"updateEmployee/:id",
          name:'updateEmployee',
          component:()=> import("@/views/admin/system-management/UpdateEmployee.vue"),
        },
        {
          path:"banner-management",
          name:'banner-management',
          component:()=> import("@/views/admin/index-management/Banner.vue"),
        },{
          path:"cate-management",
          name:'cate-management',
          component:()=> import("@/views/admin/index-management/Category.vue"),
        },{
          path:"audits-management",
          name:'audits-management',
          component:()=> import("@/views/admin/audits-management/Audits.vue"),
        },{
          path:"v-management",
          name:'v-management',
          component:()=> import("@/views/admin/video-mangegment/Video.vue"),
        }
      ]
    },
    {
      path:"/admin/login",
      name:'login',
      component:()=> import("@/views/Login.vue")
    }
  ],
})


router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  const loginStore = useLoginStore();
  // console.log(to.path)
  // console.log(from.path)
  // console.log(router)
  // console.log(router.currentRoute.value)
  const target = to.path
  console.log("目标页面路径为："+target)
  console.log("to对象信息为：")
  console.log(to)
  if(target.includes("video")) {
    if (to.params.videoId){
      next()
      return
    } else {
      next('/')
      return
    }
  }

  if(target.includes('space') || target.includes("history")
      || target.includes("dynamic") || target.includes("account") ||
      target.includes("message") || target.includes("create")){
    if(userStore.userInfo.userId || localStorage.getItem("userId")){
      // if(!target.includes('space')){
      //   console.log("开始变色")
      //   emitter.emit("changeBgColor",{color:"black",hasColor:true})
      // }
      next()
      return
    }else {
      loginStore.setPromptLogin(true)
      emitter.emit('showLogin',true)
      console.log("重定向到首页")
      next('/')
      return
    }
  }
  console.log(target+"路径放行了")
  next()
})
export default router
